package com.kxmall.admin.controller.storage;

import java.util.List;
import java.util.Arrays;

import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.storage.domain.vo.KxStorageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.storage.domain.vo.KxStorageInGoodsVo;
import com.kxmall.storage.domain.bo.KxStorageInGoodsBo;
import com.kxmall.admin.controller.storage.service.IKxStorageInGoodsService;
import com.kxmall.common.core.page.TableDataInfo;

/**
 * 商品入库
 */
@Api(tags = "商品入库")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/storage/goodsInStorage")
public class KxStorageInGoodsController extends BaseController {

    private final IKxStorageInGoodsService iKxGoodsInStorageService;
    /**
     * 查询全部入库商品
     */
    @SaIgnore
    @ApiOperation(value = "/storage/goodsInStorage/listAll - 查询全部入库商品", httpMethod = "POST")
    @PostMapping("/listAll")
    public R<List<KxStorageInGoodsVo>> listAll(KxStorageInGoodsBo bo, HttpServletResponse response) {
        List<KxStorageInGoodsVo> list = iKxGoodsInStorageService.queryList(bo);
        return R.ok(list);
    }
    /**
     * 分页查询商品入库列表
     */
    @SaCheckPermission("storage:goodsInStorage:pageList")
    @ApiOperation(value = "/storage/goodsInStorage/pageList - 分页查询商品入库列表", httpMethod = "GET")
    @GetMapping("/pageList")
    public TableDataInfo<KxStorageInGoodsVo> pageList(KxStorageInGoodsBo bo, PageQuery pageQuery) {
        return iKxGoodsInStorageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品入库列表
     */
    @SaCheckPermission("storage:goodsInStorage:export")
    @Log(title = "导出商品入库列表", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "/storage/goodsInStorage/export - 导出商品入库列表", httpMethod = "POST")
    @PostMapping("/export")
    public void export(KxStorageInGoodsBo bo, HttpServletResponse response) {
        List<KxStorageInGoodsVo> list = iKxGoodsInStorageService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品入库", KxStorageInGoodsVo.class, response);
    }

    /**
     * 获取商品入库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("storage:goodsInStorage:query")
    @ApiOperation(value = "/storage/goodsInStorage/query/{id} - 获取商品入库详细信息", httpMethod = "GET")
    @GetMapping("/query/{id}")
    public R<KxStorageInGoodsVo> query(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(iKxGoodsInStorageService.queryById(id));
    }

    /**
     * 新增商品入库
     */
    @SaCheckPermission("storage:goodsInStorage:add")
    //@Log(title = "新增商品入库", businessType = BusinessType.INSERT)
    @ApiOperation(value = "/storage/goodsInStorage/add - 新增商品入库", httpMethod = "POST")
    @PostMapping("/add")
    @RepeatSubmit()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KxStorageInGoodsBo bo) {
        return toAjax(iKxGoodsInStorageService.insertByBo(bo));
    }

    /**
     * 修改商品入库
     */
    @SaCheckPermission("storage:goodsInStorage:edit")
    //@Log(title = "修改商品入库", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "/storage/goodsInStorage/edit - 修改商品入库", httpMethod = "PUT")
    @PutMapping("/edit")
    @RepeatSubmit()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KxStorageInGoodsBo bo) {
        return toAjax(iKxGoodsInStorageService.updateByBo(bo));
    }

    /**
     * 删除商品入库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("storage:goodsInStorage:remove")
    //@Log(title = "删除商品入库", businessType = BusinessType.DELETE)
    @ApiOperation(value = "/storage/goodsInStorage/remove/{ids} - 删除商品入库", httpMethod = "DELETE")
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iKxGoodsInStorageService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 入库
     */
    @ApiOperation(value = "/storage/goodsInStorage/updateInOfStorage - 入库", httpMethod = "POST")
    @PostMapping("/updateInOfStorage")
    @RepeatSubmit()
    public R<Boolean> updateInOfStorage(@Validated(AddGroup.class) @RequestBody KxStorageInGoodsBo bo) {
        return R.ok(iKxGoodsInStorageService.updateInOfStorage(bo));
    }


    /**
     * 获取所有仓库的名称
     */
    @ApiOperation(value = "/storage/goodsInStorage/storageAllName - 获取所有仓库的名称", httpMethod = "POST")
    @PostMapping("/storageAllName")
    @RepeatSubmit()
    public R<List<KxStorageVo>> storageAllName(@Validated(AddGroup.class) @RequestBody KxStorageInGoodsBo bo) {
        return R.ok(iKxGoodsInStorageService.storageAllName(bo));
    }

}
